﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections.ObjectModel;

namespace BambolettaDB
{
    public class ManageDollsVM: BaseVM
    {
        #region Properties and Fields
        private ObservableCollection<StringItemVM> _Hair = new ObservableCollection<StringItemVM>();
        public ObservableCollection<StringItemVM> Hair
        {
            get { return _Hair; }
            set { _Hair = value; NotifyPropertyChanged("Hair"); }
        }

        private ObservableCollection<StringItemVM> _HairTypes = new ObservableCollection<StringItemVM>();
        public ObservableCollection<StringItemVM> HairTypes
        {
            get { return _HairTypes; }
            set { _HairTypes = value; NotifyPropertyChanged("HairTypes"); }
        }

        private ObservableCollection<StringItemVM> _Eyes = new ObservableCollection<StringItemVM>();
        public ObservableCollection<StringItemVM> Eyes
        {
            get { return _Eyes; }
            set { _Eyes = value; NotifyPropertyChanged("Eyes"); }
        }

        private ObservableCollection<StringItemVM> _Skin = new ObservableCollection<StringItemVM>();
        public ObservableCollection<StringItemVM> Skin
        {
            get { return _Skin; }
            set { _Skin = value; NotifyPropertyChanged("Skin"); }
        }

        private ObservableCollection<StringItemVM> _Gender = new ObservableCollection<StringItemVM>();
        public ObservableCollection<StringItemVM> Gender
        {
            get { return _Gender; }
            set { _Gender = value; NotifyPropertyChanged("Gender"); }
        }

        private ObservableCollection<StringItemVM> _Types = new ObservableCollection<StringItemVM>();
        public ObservableCollection<StringItemVM> Types
        {
            get { return _Types; }
            set { _Types = value; NotifyPropertyChanged("Types"); }
        }

        private ObservableCollection<StringItemVM> _Locations = new ObservableCollection<StringItemVM>();
        public ObservableCollection<StringItemVM> Locations
        {
            get { return _Locations; }
            set { _Locations = value; NotifyPropertyChanged("Locations"); }
        }

        private ObservableCollection<StringItemVM> _Status = new ObservableCollection<StringItemVM>();
        public ObservableCollection<StringItemVM> Status
        {
            get { return _Status; }
            set { _Status = value; NotifyPropertyChanged("Status"); }
        }
        #endregion

        #region Constructor
        public ManageDollsVM()
        {
            if (!App.IsInDesignMode)
            {
                Hair = DBCommands.GetHair();
                Hair.CollectionChanged += new System.Collections.Specialized.NotifyCollectionChangedEventHandler(Hair_CollectionChanged);
                HairTypes = DBCommands.GetHairTypes();
                HairTypes.CollectionChanged += new System.Collections.Specialized.NotifyCollectionChangedEventHandler(HairTypes_CollectionChanged);
                Eyes = DBCommands.GetEyes();
                Eyes.CollectionChanged += new System.Collections.Specialized.NotifyCollectionChangedEventHandler(Eyes_CollectionChanged);
                Skin = DBCommands.GetSkin();
                Skin.CollectionChanged += new System.Collections.Specialized.NotifyCollectionChangedEventHandler(Skin_CollectionChanged);
                Gender = DBCommands.GetGender();
                Gender.CollectionChanged += new System.Collections.Specialized.NotifyCollectionChangedEventHandler(Gender_CollectionChanged);
                Types = DBCommands.GetTypes();
                Types.CollectionChanged += new System.Collections.Specialized.NotifyCollectionChangedEventHandler(Types_CollectionChanged);
                Locations = DBCommands.GetLocations();
                Locations.CollectionChanged += new System.Collections.Specialized.NotifyCollectionChangedEventHandler(Locations_CollectionChanged);
                Status = DBCommands.GetStatus();
                Status.CollectionChanged += new System.Collections.Specialized.NotifyCollectionChangedEventHandler(Status_CollectionChanged);
            }
        }

        void Status_CollectionChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
        {
            StringItem_CollectionChanged(sender, "Status", "Description", e);
        }

        void Locations_CollectionChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
        {
            StringItem_CollectionChanged(sender, "Location", "Description", e);
        }

        void HairTypes_CollectionChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
        {
            StringItem_CollectionChanged(sender, "HairType", "Description", e);
        }

        void Types_CollectionChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
        {
            StringItem_CollectionChanged(sender, "Type", "Description", e);
        }

        void Gender_CollectionChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
        {
            StringItem_CollectionChanged(sender, "Gender", "Name", e);
        }

        void Skin_CollectionChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
        {
            StringItem_CollectionChanged(sender, "Skin", "Color", e);
        }

        void Hair_CollectionChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
        {
            StringItem_CollectionChanged(sender, "Hair", "Color", e);
        }

        void Eyes_CollectionChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
        {
            StringItem_CollectionChanged(sender, "Eyes", "Color", e);
        }

        private void StringItem_CollectionChanged(object sender, string table, string column, System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
        {
            if (e.NewItems != null)
            {
                foreach (StringItemVM item in e.NewItems)
                {
                    item.SourceColumn = column;
                    item.SourceTable = table;
                    DBCommands.InsertStringItem(item);
                }
            }

            if (e.OldItems != null)
            {
                foreach (StringItemVM item in e.OldItems)
                {
                    DBCommands.DeleteStringItem(item);
                }
            }
        }
        #endregion


    }
}
